package com.qiniuyun.backai.config;

import com.qiniuyun.backai.interceptor.myInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfiguration implements WebMvcConfigurer {
    @Autowired
    private myInterceptor interceptor;
    //解决跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")//设置允许跨域请求的源（域名
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")//作用：设置允许的 HTTP 请求方法
                .allowedHeaders("*")
                .exposedHeaders("Content-Disposition");
    }
    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry) {
        interceptorRegistry.addInterceptor(interceptor)
                .addPathPatterns("/**")//拦截所有的的请求
                .excludePathPatterns("/user/login", "/user/register")//放行
                .excludePathPatterns(
                        // Knife4j 文档相关
                        "/doc.html/**",           // 文档页面
                        "/webjars/**",            // 静态资源
                        "/swagger-resources/**",  // Swagger 资源
                        "/v3/api-docs/**",        // OpenAPI 规范端点
                        "/favicon.ico"            // 网站图标
                );

    }
}
